from write.AbstractWrite import AbstractWrite
import shutil
import os


class CopyContract(AbstractWrite):
	def __init__(self):
		super().__init__()
		self.type = "cargo"
		self.oldPath = ""

	def setDic(self, cargoTextDic, cargoUnderlineDic, shipTextDic, shipUnderlineDic):
		self.cargoTextDic = cargoTextDic
		self.cargoUnderlineDic = cargoUnderlineDic
		self.shipTextDic = shipTextDic
		self.shipUnderlineDic = shipUnderlineDic

	def setType(self, type):
		self.type = type

	def setOldPath(self, oldPath):
		self.oldPath = oldPath

	def write(self):
		oldName = os.path.split(self.oldPath)[1]

		if self.type == "cargo":
			outputPath = os.path.join(self.outputDir,
			                          "{shipName}V{voyage}".format(shipName=self.cargoUnderlineDic["航次"][0],
			                                                       voyage=self.cargoUnderlineDic["航次"][1]))
			# newname = "货附件1：{shipName}V{voyage}{oldname}".format(shipName=self.cargoUnderlineDic["航次"][0],
			#                                                     voyage=self.cargoUnderlineDic["航次"][1],
			#                                                     oldname=oldName)
			"""V1.3新需求合同不需要自动生成船名航次号"""
			newname = "货附件1：{oldname}".format(oldname=oldName)
		else:
			outputPath = os.path.join(self.outputDir,
			                          "{shipName}V{voyage}".format(shipName=self.shipUnderlineDic["航次"][0],
			                                                       voyage=self.shipUnderlineDic["航次"][1]))
			# newname = "船附件1：{shipName}V{voyage}{oldname}".format(shipName=self.shipUnderlineDic["航次"][0],
			#                                                         voyage=self.shipUnderlineDic["航次"][1],
			#                                                         oldname=oldName)
			"""V1.3新需求合同不需要自动生成船名航次号"""
			newname = "船附件1：{oldname}".format(oldname=oldName)

		if not os.path.exists(outputPath):
			os.makedirs(outputPath)
		newPath = os.path.join(outputPath, newname)

		if not os.path.exists(newPath):
			shutil.copy(self.oldPath, newPath)  # 复制文件
